<?php
include_once '../includes/common.php';
if($islogin2==1){}else sysmsg("您还未登录，请先登录",2,'./login.php',true);
$title='用户竞猜下注';
include_once './header.php';
$count=$DB->count("SELECT count(*) from authguao_guess WHERE uid={$userrow['uid']}");
if($conf['guess_open']!=1)showmsg('错误', '授权平台未开放娱乐竞猜功能！', 4, './');
?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                当前第<?php echo $conf['guess_stage']?>期
            </div>
            <div class="card-body">
                <div class="layui-elem-quote">当前中奖资金是下注资金的<?php echo $conf['guess_multiple']?>倍<hr>最小可下注金额<?php echo $conf['guess_xz_min']?>元<hr>最大可下注金额<?php echo $conf['guess_xz_max']?>元</div>
                <form class="form-horizontal layui-form">
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">下注选手：</label>
                        <select class="form-control" name="type" lay-search lay-filter="type"><option value="1">①号选手</option><option value="2">②号选手</option><option value="3">③号选手</option><option value="4">④号选手</option><option value="5">⑤号选手</option><option value="6">⑥号选手</option><option value="7">⑦号选手</option><option value="8">⑧号选手</option><option value="9">⑨号选手</option></select>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">下注金额：</label>
                       <input type="text" class="form-control" name="money" value="1" min="1"  lay-verType="tips" lay-verify="required"/>
                    </div>
                    <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_guess">下 注</button>
                </form>
            </div>
        </div>
        <div class="card">
            <div class="card-header">
                我的竞猜记录
            </div>
            <div class="card-body">
                <div class="layui-elem-quote">我共有[<?php echo $count?>]个竞猜历史</div>
                    <div style="white-space:nowrap;overflow-x: auto;">
                        <table class="layui-table layuiadmin-page-table">
                            <thead><tr><th>ID</th><th>期数</th><th>押注</th><th>胜者</th><th>结果</th><th>资金</th><th>获奖</th><th>操作</th></tr></thead>
                            <tbody>
                            <?php
                            $pagesize=30;
                            $pages=ceil($numrows/$pagesize);
                            $page=isset($_GET['page'])?intval($_GET['page']):1;
                            $offset=$pagesize*($page - 1);
                            $rs=$DB->query("SELECT * FROM authguao_guess WHERE uid='{$userrow['uid']}' order by id desc limit $offset,$pagesize");
                            while($res = $DB->fetch($rs))
                            {
                                if($res['yzhm']=='1'){
                                    $iuser='①号选手';
                                }else if($res['yzhm']=='2'){
                                    $iuser='②号选手';
                                }else if($res['yzhm']=='3'){
                                    $iuser='③号选手';
                                }else if($res['yzhm']=='4'){
                                    $iuser='④号选手';
                                }else if($res['yzhm']=='5'){
                                    $iuser='⑤号选手';
                                }else if($res['yzhm']=='6'){
                                    $iuser='⑥号选手';
                                }else if($res['yzhm']=='7'){
                                    $iuser='⑦号选手';
                                }else if($res['yzhm']=='8'){
                                    $iuser='⑧号选手';
                                }else if($res['yzhm']=='9'){
                                    $iuser='⑨号选手';
                                }else{
                                    $iuser='未知选手';
                                }
                                if($res['hm']=='1'){
                                    $islj='①号选手';
                                }else if($res['hm']=='2'){
                                    $islj='②号选手';
                                }else if($res['hm']=='3'){
                                    $islj='③号选手';
                                }else if($res['hm']=='4'){
                                    $islj='④号选手';
                                }else if($res['hm']=='5'){
                                    $islj='⑤号选手';
                                }else if($res['hm']=='6'){
                                    $islj='⑥号选手';
                                }else if($res['hm']=='7'){
                                    $islj='⑦号选手';
                                }else if($res['hm']=='8'){
                                    $islj='⑧号选手';
                                }else if($res['hm']=='9'){
                                    $islj='⑨号选手';
                                }else{
                                    $islj='请敬请期待';
                                }
                                if(!$res['hm']){
                                    $isuse='未开奖，请耐心等待';
                                    $flmoney=$res['money'];
                                }else if($res['yzhm']==$res['hm']){
                                    $isuse='<font color="green">恭喜您，中奖啦</font>';
                                    $flmoney=$res['money']*$conf['guess_multiple'];
                                }else{
                                    $isuse='<font color="red">很遗憾，未中奖</font>';
                                    $flmoney='0.00';
                                }
                                if($res['yzhm']==$res['hm'] && $res['prize']=='0'){
                                    $lingqu='<span class="layui-btn layui-btn-xs btn-success" onclick="guess_lq('.$res['id'].')">领取奖励</span>';
                                }else{
                                    $lingqu='';
                                }
                                echo '<tr><td>'.$res['id'].'</td><td>第'.$res['jcqs'].'期</td><td>'.$iuser.'</td><td>'.$islj.'</td><td>'.$isuse.'</td><td>'.$res['money'].'元</td><td>'.$flmoney.'元</td><td>'.$lingqu.'<span onclick="guessdel(' . $res['id'] . ')" class="layui-btn layui-btn-xs btn-danger">删除</span></td></tr>';
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
                <div class="text-center">
                <?php
                #分页
                $pageList=new Page($numrows,$pagesize,0,$link);
                echo $pageList->showPage();
                ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
include_once './bottom.php';
?>
<script type="text/javascript">
layui.use(['form'], function () {
    var form = layui.form;
    form.on('submit(submit_guess)', function (data) {
        layer.alert('是否要执行当前操作？', {
            icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                guess_xz();
            }
        });
        return false;
    });
});
function guess_xz() {
    var type = $("select[name='type']").val();
    var money = $("input[name='money']").val();
    var ii = layer.msg('正在下注中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "POST",
        url: "ajax.php?act=guess_xz",
        data : {type:type,money:money},
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.reload();
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
function guess_lq(id) {
    var ii = layer.msg('正在领取中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "GET",
        url: "ajax.php?act=guess_lq&id="+id,
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.reload();
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
function guessdel(id) {
    var confirmobj = layer.confirm('你确实要删除这条竞猜记录吗？', {
      btn: ['确定','取消']
    }, function(){
      $.ajax({
        type : 'GET',
        url : 'ajax.php?act=guessdel&id='+id,
        dataType : 'json',
        success : function(data) {
            if(data.code == 0){
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.reload();
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
      });
    }, function(){
      layer.close(confirmobj);
    });
}
</script>